package nju.bishe.weibo;

import nju.bishe.weibo.data.Position;
import nju.bishe.weibo.ui.base.Refreshable;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.android.maps.MapActivity;

public class DrinkeryActivity extends MapActivity{
	private static final String TAG = "DrinkeryActivity";

	private Button backButton;
	protected ImageButton refreshButton;
	protected ImageButton searchButton;
	protected ImageButton writeButton;
	protected ImageButton laughButton;
	private Position currentLocation;
	private TextView tv_current;
	private TextView tv_addr;
	private TextView tv_tel;
	private TextView tv_nav;

	protected void onCreate(Bundle savedInstanceState){
		Log.i(TAG, "onCreate()...");
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_PROGRESS);
		
		setContentView(R.layout.drinkery_page);
		initHeader();
		

		currentLocation = getIntent().getParcelableExtra("positionId");
		tv_current=(TextView)findViewById(R.id.drinkery_current_location);
		tv_addr=(TextView)findViewById(R.id.drinkery_addr);
		tv_tel=(TextView)findViewById(R.id.drinkery_tel);
		tv_nav=(TextView)findViewById(R.id.drinkery_nav);
		tv_current.setText(currentLocation.getName());
		tv_addr.setText(currentLocation.getAddress());
		String temp=currentLocation.getTelephone();
		if(!temp.equals(""))
		   tv_tel.setText(currentLocation.getTelephone());
		else
			tv_tel.setText("未知");
		tv_nav.setText(currentLocation.getNavigator());
	}


    protected void initHeader() {
		addHeaderView(R.layout.header_back);
		
		//addBackButton();
		backButton = (Button) findViewById(R.id.top_back);
		backButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Go back to previous activity
				finish();
			}
		});
		
		addRefreshButton();		
		addSearchButton();
		addWriteButton();
		addLaughButton();
	}


    private void addHeaderView(int resource) {
 	// find content root view
 	ViewGroup root =  (ViewGroup) getWindow().getDecorView();
 	ViewGroup content = (ViewGroup) root.getChildAt(0);
 	View header = View.inflate(DrinkeryActivity.this, resource, null);
// 	LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
 	
 	content.addView(header, 0);
 	content.removeViewAt(1);
     }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private void addRefreshButton(){
		final Activity that = this;
		refreshButton = (ImageButton) findViewById(R.id.top_refresh);
		
		refreshButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 旋转动画
				animRotate(v);
				
				if (that instanceof Refreshable) {
					((Refreshable) that).doRetrieve();
				} else {
					Log.e(TAG, "The current view " + that.getClass().getName() + " cann't be retrieved");
				}
			}
		});
	}
	
	protected void animRotate(View v) {
		if (null != v) {
			Animation anim = AnimationUtils.loadAnimation(v.getContext(),
					R.anim.rotate360);
			v.startAnimation(anim);
		}
	}

	// 搜索
	protected void addSearchButton() {
		searchButton  = (ImageButton) findViewById(R.id.search);
		searchButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startSearch();
			}
		});
	}
	
	// 这个方法会在SearchActivity里重写
	protected boolean startSearch() {
		Intent intent = new Intent();
		intent.setClass(this, SearchActivity.class);
		startActivity(intent);
		return true;
	}

	protected void addWriteButton() {
		writeButton = (ImageButton) findViewById(R.id.writeMessage);
		
		writeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 动画
				Animation anim = AnimationUtils.loadAnimation(v.getContext(),
						R.anim.scale_lite);
				v.startAnimation(anim);
				startWrite();
				// forward to write activity
				
			}
		});
	}
	protected void startWrite() {
		Intent intent = new Intent();
		intent.setClass(this, WriteActivity.class);
		startActivity(intent);
	}

	protected void addLaughButton(){
		laughButton=(ImageButton)findViewById(R.id.laugh);
		laughButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(v.getContext(), LaughChannelActivity.class);
				v.getContext().startActivity(intent);
			}
		});
	}
}
